﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Budgetsch.WP7.Model.Live
{
    using Budgetsch.WP7.Model.DataService;
    using Budgetsch.WP7.ViewModel;
    using Budgetsch.WP7.WP7AuthenticationService;
    using Budgetsch.WP7.WP7DataService;

    public class AuthenticationService : IAuthenticationService
    {
        private readonly CookieContainerViewModel _cookieContainer;

        public AuthenticationService(CookieContainerViewModel cookieContainer)
        {
            _cookieContainer = cookieContainer;
        }

        #region Implementation of IAuthenticationService

        public void Login(string userName, string password, Action<bool, Exception> callback)
        {
            var client = new AuthenticationServiceClient();
            //client.CookieContainer = SimpleIoc.Default.GetInstance<CookieContainer>();

            client.LoginCompleted += (s,e) =>
                {
                    _cookieContainer.CookieContainer = (s as AuthenticationServiceClient).CookieContainer;
                    if (e.Error != null)
                    {
                        callback(false, e.Error);
                    }
                    else
                    {
                        callback(e.Result, null);
                    }
                };
            
            client.LoginAsync(userName, password, null, true);

        }

        #endregion
    }
}
